﻿using Dapper;
using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagChina.Basic.CCT.Db.Printer_Db;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Untilly;

namespace Proxy
{
    public class InsertInfoHandle
    {
        private SearchInfoHandle Handle_Search;

        public InsertInfoHandle()
        {
            Handle_Search = new SearchInfoHandle();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns></returns>
        public bool InsertBoradInfo(T_BoardInfo t)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_BoardInfo.Add(t);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Insert.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool InsertSettingInfos(T_SettingsConfig t)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_SettingsConfig.Add(t);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Insert.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="T"></param>
        /// <returns></returns>
        public bool InsertOnlinePart(T_TransOnlineRecords T)
        {
            bool result = false;
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_TransOnlineRecords.Add(T);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Insert.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="T"></param>
        /// <returns></returns>
        public bool InsertMprRecord(string Reference)
        {
            bool result = false;

            try
            {
                T_SendRecords_Mpr t_SendRecords_Mpr = new T_SendRecords_Mpr()
                {
                    Reference = Reference,
                    MprFlag = 1,
                    SendTime = DateTime.Now,
                };
                using (Db_CCT Db = new Db_CCT())
                {
                    Db.T_SendRecords_Mpr.Add(t_SendRecords_Mpr);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Insert.Error(ex.Message);
                return result;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="T"></param>
        /// <returns></returns>
        public bool InsertLabelTask(string BatchId)
        {
            bool result = false;
            List<T_Nesting_LabelTask> lis_LabelTask = new List<T_Nesting_LabelTask>();
            try
            {
                using (Db_CCT Db = new Db_CCT())
                {
                    string ConnectString = "Data Source=MT259730180M11;Initial Catalog=Nesting;User ID=hgservice;Password=Homag";
                    string sql_Search = "Select * from View_NestingBatchDetailInfo order by Board_Id ,Number ";
                    List<Model_NestingLabelTask> PartsInfo = new List<Model_NestingLabelTask>();
                    using (var connection = new SqlConnection(ConnectString))
                    {
                        try
                        {
                            connection.Open();
                            PartsInfo = connection.Query<Model_NestingLabelTask>(sql_Search).ToList();
                            connection.Close();
                        }
                        catch (Exception ex)
                        {
                            connection.Close();
                        }
                    }
                    new DeleteInfoHandle().DeleteAllTasks();
                    foreach (var item in PartsInfo)
                    {
                        T_Nesting_LabelTask t_Nesting_LabelTask = new T_Nesting_LabelTask()
                        {
                            BatchId = item.BatchId,
                            MprName = item.MprName,
                            Reference = item.PartId,
                            Number = item.Number,
                            Title = item.LabelTitle,
                            State = (int)EnumOfPartState.等待执行,
                            StartTime = DateTime.Now,
                            BoardInfoId = item.Board_Id
                        };
                        lis_LabelTask.Add(t_Nesting_LabelTask);
                    }
                    Db.T_Nesting_LabelTask.AddRange(lis_LabelTask);
                    result = Db.SaveChanges() > 0;
                }
                return result;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_Insert.Error(ex.Message);
                return result;
            }
        }
    }
}